package com.juejin.www.controller;

import com.juejin.www.aspect.Log;
import com.juejin.www.aspect.Perm;
import com.juejin.www.model.DTO.BoilingPointDTO;
import com.juejin.www.model.DTO.PageDTO;
import com.juejin.www.model.DTO.Result;
import com.juejin.www.model.VO.BoilingPointVO;
import com.juejin.www.service.BoilingPointService;
import com.juejin.www.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Jacky
 */
@Slf4j
@RestController
@RequestMapping("/boilingPoint")
public class BoilingPointController {

    @Autowired
    private BoilingPointService boilingPointService;

    @GetMapping
    @Perm(permission = "boilingPoint:read")
    public Result list(@RequestParam Integer begin,@RequestHeader("token") String jwt){
        Integer uid = null;
        if(jwt!=null){
            try{
                uid = JwtUtils.getIdByJWT(jwt);
            }catch (Exception e){
                uid=null;
            }
        }
        log.info("获取沸点列表");
        PageDTO<BoilingPointVO> boilingPointVos = boilingPointService.list(begin,uid);
        return Result.success(boilingPointVos);
    }

    @Log
    @Perm(permission = "boilingPoint:add")
    @PostMapping
    public Result add(@RequestBody BoilingPointDTO boilingPointDTO,@RequestHeader("token") String jwt){
        Integer uid = JwtUtils.getIdByJWT(jwt);
        boilingPointDTO.setAuthorId(uid);
        log.info("{}号用户发布沸点",boilingPointDTO.getAuthorId());
        boilingPointService.addBoilingPoint(boilingPointDTO);
        return Result.success();
    }

    @Log
    @Perm(permission = "boilingPoint:delete")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除{}号沸点",id);
        boilingPointService.deleteBoilingPoint(id);
        return Result.success();
    }

}
